<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .k{
            width: 350px;
            height: 200px;
            border: 1px solid black;
            margin: 50px auto;
            position: relative;
        }
        .yk{
            width: 100px;
            height: 20px;
            /* background-color: black; */
            position: absolute;
            bottom: 15px;
            left: 50%;
            right: 50%;
            margin-left: -50px;
        }
        .yk div{
            width: 20px;
            height: 20px;
            background-color: red;
            float: left;
            border-radius: 50%;
            margin-left: 5px;
        }
        img{
            width: 350px;
            height: 200px;
            display: block;
            position: absolute;
            top: 0px;
        }
        .left{
            width: 30px;
            height: 30px;
            position: absolute;
            left: 0px;
            top: 50%;
            bottom: 50%;
            margin-top: -15px;
        }
        .right{
            width: 30px;
            height: 30px;
            position: absolute;
            right: 0px;
            top: 50%;
            bottom: 50%;
            margin-top: -15px;
        }
        .active{
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="k">
        <img src="./img/4.jpg" alt="">
        <div class="yk">
            <div class="q" class="active"></div>
            <div class="q"></div>
            <div class="q"></div>
            <div class="q"></div>
        </div>
        <button class="left">左</button>
        <button class="right">右</button>
    </div>

    <script>
        // 1、获取元素
        var oImg = document.querySelector("img");
        var oQ = document.getElementsByClassName('q');
        var oBtn = document.querySelectorAll('button');

        for(var i=0;i<oQ.length;i++){
            oQ[i].index = i;
            oQ[i].onclick = function(){
                oImg.src = './img/'+ (this.index+1) +'.jpg';
            }
        }
    </script>
</body>
</html>